<?php
namespace App\Service\Order;

use App\Models\ConfigureFunction; //功能配置表

use GuzzleHttp\Client;

class ApiService
{

    private static function request($path, $args_data = [])
    {

        //http实例
        $client = new Client();

        //查询商城配置域名
        $domain_url = ConfigureFunction::getConfigureField(43, 'domain_url');

        $res = $client->get($domain_url . $path, ['query' => $args_data]);
        if ($res->getStatusCode() != 200) {
            return ['code' => 10001, 'message' => '接口调用失败'];
        }

        return json_decode($res->getBody()->getContents(), true);

    }

    /**
     * 订单异步实时传入ERP
     * @param array $args_data
     * @return array
     */
    public static function billErp( $args_data )
    {

        $res = self::request('/async/bill/bill.erp.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }

    /**
     * 订单配送消息异步推送
     * @param $args_data
     * @return array
     */
    public static function billProcess( $args_data )
    {

        $res = self::request('/async/bill/bill.process.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }


    /**
     * 订单异步推送脚本进行积分、优惠券的发放、库存扣减
     * @param $args_data
     * @return array
     */
    public static function billFinish( $args_data )
    {

        $res = self::request('/async/bill/bill.finish.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }


    /**
     * 订单异步推送脚本进行积分、优惠券的发放、库存扣减
     * @param $args_data
     * @return array
     */
    public static function billMessageTemplate( $args_data )
    {

        $res = self::request('/async/message/bill_message_template.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }

    /**
     * 订单取消生成退款单
     * @param $args_data
     * @return array
     */
    public static function storeOrderReturn( $args_data )
    {

        $res = self::request('/app/api/return_order.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }


}